package calao;

import calao.storage.Field;
import calao.storage.Record;
import calao.storage.Sequence;
import calao.storage.Storage;
import calao.storage.Table;

class EventLogDB 
{

	static final Table TABLE = new Table("calao.Event"); 

	static final Sequence IDEXT_SEQUENCE = new Sequence("calao.EventIdExt");
	
	public static final Field<Long> ID = 
		TABLE.createField("id", Field.LONG);	

	public static final Field<Long> GUID = 
		TABLE.createField("guid", Field.LONG);	

	public static final Field<String> APPLICATION = 
		TABLE.createField("app", Field.STRING);	

	public static final Field<String> EVENT_NAME = 
		TABLE.createField("eventName", Field.STRING);	

	public static final Field<String> ARGV = 
		TABLE.createField("argv", Field.STRING, true);	

	static {
		TABLE.createPrimaryKeyIndex(ID);
	}

	static long newId(Storage storage) {
		int idext = storage.getNextValue(IDEXT_SEQUENCE);
		return - ( ((System.currentTimeMillis()/60000)<<32) | idext );
	}
	
	static int getTimeMinutes(long id) {
		return (int)((-id)>>32);
	}
	
	static void log(Storage storage,  String appName, String eventName, Long guid, String[] argv)
	{
		Record record = new Record(TABLE);
		record.set(ID, newId(storage));
		record.set(GUID, guid);
		record.set(APPLICATION, appName);
		record.set(EVENT_NAME, eventName);
		record.set(ARGV, argv);
		storage.store(record);
	}
	
}
